/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui.db;

import java.io.IOException;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import blueprint4j.db.Entity;
import blueprint4j.db.FieldGroup;
import blueprint4j.db.FieldGroupBar;
import blueprint4j.db.VectorFieldGroup;
import blueprint4j.gui.Binder;
import blueprint4j.gui.DataPanel;
import blueprint4j.report.db.FieldReportGroup;
import blueprint4j.report.db.VectorFieldReportGroup;
import blueprint4j.utils.BindException;
import blueprint4j.utils.Bindable;

public class DataPanelEntity extends DataPanel {
	private static final long serialVersionUID = 1L;
	
	private String name;
	private VectorFieldGroup groups=null;
	private VectorFieldReportGroup rgroup=null;
	
	public DataPanelEntity(String name,Entity entity){
		this(name,entity.getFieldGroups(),entity.getFieldReportGroups());
	}

	public DataPanelEntity(String name,FieldGroup[] groups,FieldReportGroup [] rgroups){
		this(name,new VectorFieldGroup(groups),new VectorFieldReportGroup(rgroups));
	}

	public DataPanelEntity(String name,VectorFieldGroup groups,VectorFieldReportGroup rgroup){
		this.groups=groups;
		this.rgroup = rgroup;
		this.name=name;
	}

	public String getName(){
		return name;
	}

	public void activated() {}
	public void deactivated() {}

	public void build(Binder binder, Bindable entity)
	throws IOException, BindException {
		VectorFieldGroup egroups=((Entity)entity).getFieldGroups();
		Iterator<FieldGroupBar> fieldGroupBars=((Entity)entity).getFieldGroupBars().iterator();
		
		Box currentBox=null;
		JTabbedPane currentTab=null;
		int columnCount=0;
		FieldGroupBar currentBar=null;
		
		for (int g=0;g<groups.size();g++){
			FieldGroup group=groups.get(g);
			if (currentBar==null && fieldGroupBars.hasNext()){
				currentBar=fieldGroupBars.next();
				columnCount=currentBar.getColumns();
				if (currentBar.getType()==FieldGroupBar.BAR_TYPE.BOXES){
					currentBox=new Box(SwingConstants.HORIZONTAL);//columnCount);
				}else{
					currentTab=new JTabbedPane();
				}
			}
			for (int t=0;t<egroups.size();t++){
				if (group.equals(egroups.get(t))){
					if (columnCount>0 && currentBar!=null && currentBar.contains(group)){
						columnCount--;
						if (currentBox!=null){
							currentBox.add(binder.createDataGroup(egroups.get(t)));
						}else{
							currentTab.addTab(egroups.get(t).getName(),binder.createDataGroup(egroups.get(t)));
						}
						if (columnCount==0){
							if (currentBox!=null){
								add(currentBox);
								currentBox=null;
							} else{
								add(currentTab);
								currentTab=null;
							}
							currentBar=null;
						}
					}else{
						addGroup(binder.createDataGroup(egroups.get(t)));
					}
					break;
				}
			}
		}
		// CREATE THE REPORT GROUPS
		VectorFieldReportGroup vfg=((Entity)entity).getFieldReportGroups();
		for (int g=0;g<rgroup.size();g++){
			FieldReportGroup group=rgroup.get(g);
			for (int t=0;t<vfg.size();t++){
				if (group.equals(vfg.get(t))){
					addGroup(binder.createDataGroup(vfg.get(t)));
					break;
				}
			}
		}
	}
}

